cmake_minimum_required(VERSION 2.8.3)
project(lci_slam)

SET(CMAKE_BUILD_TYPE "Release")

ADD_COMPILE_OPTIONS(-std=c++14 )
ADD_COMPILE_OPTIONS(-std=c++14 )
set( CMAKE_CXX_FLAGS "-std=c++14 -O3" )

add_definitions(-w)
add_definitions(-DROOT_DIR=\"${PROJECT_SOURCE_DIR}/\")

LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

# add_definitions(-DROOT_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/\")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexceptions" )
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread -std=c++0x -std=c++14 -fexceptions")

message("Current CPU archtecture: ${CMAKE_SYSTEM_PROCESSOR}")
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)" )
  include(ProcessorCount)
  ProcessorCount(N)
  message("Processer number:  ${N}")
  if(N GREATER 4)
    add_definitions(-DMP_EN)
    add_definitions(-DMP_PROC_NUM=3)
    message("core for MP: 3")
  elseif(N GREATER 3)
    add_definitions(-DMP_EN)
    add_definitions(-DMP_PROC_NUM=2)
    message("core for MP: 2")
  else()
    add_definitions(-DMP_PROC_NUM=1)
  endif()
else()
  add_definitions(-DMP_PROC_NUM=1)
endif()

# OpenMP
find_package(OpenMP QUIET)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}   ${OpenMP_C_FLAGS}")

# PythonLibs
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
message(PythonLibs [OK]: ${PYTHON_INCLUDE_DIRS})
find_path(MATPLOTLIB_CPP_INCLUDE_DIRS "matplotlibcpp.h")

# catkin
find_package(catkin REQUIRED COMPONENTS
  geometry_msgs
  nav_msgs
  sensor_msgs
  roscpp
  rospy
  std_msgs
  pcl_ros
  tf
  message_generation
  eigen_conversions
  cv_bridge
  rosbag
  image_transport
  pcl_conversions
  pcl_msgs
)
include_directories(${catkin_INCLUDE_DIRS})
message(catkin [OK]: ${catkin_INCLUDE_DIRS})

add_message_files(
  DIRECTORY msg
  FILES 
  Pose6D.msg
  altimeter.msg
  encoder.msg
  fog_3axis.msg
  fog.msg
  imu.msg
  LaserScanArray.msg
  vrs.msg
  CustomPoint.msg
  CustomMsg.msg
)

generate_messages(
 DEPENDENCIES
 geometry_msgs
 std_msgs
 sensor_msgs
 nav_msgs
)

catkin_package(
  CATKIN_DEPENDS geometry_msgs nav_msgs roscpp rospy std_msgs message_runtime message_generation
  DEPENDS EIGEN3 PCL
  INCLUDE_DIRS
)

# Eigen3
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
message(Eigen [OK]: ${EIGEN3_INCLUDE_DIR})

# PCL
find_package(PCL REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
message(PCL [OK]: ${PCL_INCLUDE_DIRS})

# Boost
find_package(Boost REQUIRED filesystem serialization regex timer date_time thread)
include_directories(${Boost_INCLUDE_DIRS})
message(Boost [OK]: ${Boost_INCLUDE_DIRS})

# GTSAM
find_package(GTSAM REQUIRED)
include_directories(${GTSAM_INCLUDE_DIR})
message(GTSAM [OK]: ${GTSAM_INCLUDE_DIR})

# GLOG 
find_package(GLOG REQUIRED)
include_directories(${GLOG_INCLUDE_DIRS})
message(GLOG [OK]: ${GLOG_INCLUDE_DIRS})

# # MKL
# find_package(MKL REQUIRED)
# include_directories(${MKL_INCLUDE_DIRS})
# message(MKL [OK]: ${MKL_INCLUDE_DIRS})

# # TBB
# find_package(TBB REQUIRED)

# yaml-cpp
find_package(yaml-cpp REQUIRED)

# Ceres
find_package(Ceres REQUIRED)
include_directories(${CERES_INCLUDE_DIRS})
message(Ceres [OK]: ${CERES_INCLUDE_DIRS})

# OpenCV
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
message(OpenCV [OK]: ${OpenCV_VERSION})

# Include
set(LCISLAM_SRC_DIRS ${PROJECT_SOURCE_DIR}/src/)
include_directories(
  ${LCISLAM_SRC_DIRS}
)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)

######################################################
##################### ThirdParty #####################
######################################################

add_subdirectory(thirdparty/Sophus)
include_directories(thirdparty/Sophus)

# file(GLOB LCISLAM_CAMERA_Files
#   ${LCISLAM_SRC_DIRS}/lci_slam/camera/*.h
#   ${LCISLAM_SRC_DIRS}/lci_slam/camera/*.cc
#   ${LCISLAM_SRC_DIRS}/lci_slam/camera/*.cpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/camera/*.hpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/camera/*/*.h
#   ${LCISLAM_SRC_DIRS}/lci_slam/camera/*/*.cc
#   ${LCISLAM_SRC_DIRS}/lci_slam/camera/*/*.cpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/camera/*/*.hpp
# )

# file(GLOB LCISLAM_LIDAR_Files
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*.h
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*.cc
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*.cpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*.hpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*.h
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*.cc
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*.cpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*.hpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*/*.h
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*/*.cc
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*/*.cpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*/*.hpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*/*/*.h
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*/*/*.cc
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*/*/*.cpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/lidar/*/*/*/*.hpp
# )

# file(GLOB LCISLAM_GTSAM_BACKEND_Files
#   ${LCISLAM_SRC_DIRS}/lci_slam/gtsam_backend/*.h
#   ${LCISLAM_SRC_DIRS}/lci_slam/gtsam_backend/*.cc
#   ${LCISLAM_SRC_DIRS}/lci_slam/gtsam_backend/*.cpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/gtsam_backend/*.hpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/gtsam_backend/*/*.h
#   ${LCISLAM_SRC_DIRS}/lci_slam/gtsam_backend/*/*.cc
#   ${LCISLAM_SRC_DIRS}/lci_slam/gtsam_backend/*/*.cpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/gtsam_backend/*/*.hpp
# )

# file(GLOB LCISLAM_Files
#   ${LCISLAM_SRC_DIRS}/lci_slam/*.h
#   ${LCISLAM_SRC_DIRS}/lci_slam/*.cc
#   ${LCISLAM_SRC_DIRS}/lci_slam/*.cpp
#   ${LCISLAM_SRC_DIRS}/lci_slam/*.hpp
#   ${LCISLAM_CAMERA_Files}
#   ${LCISLAM_LIDAR_Files}
#   # ${LCISLAM_GTSAM_BACKEND_Files}
# )

# add_library(${PROJECT_NAME} SHARED
#   ${LCISLAM_Files}
# )

# target_link_libraries(${PROJECT_NAME}
#   ${OpenCV_LIBS}
#   ${EIGEN3_LIBS}
#   ${Boost_LIBRARIES}
#   ${GLOG_LIBRARIES}
#   ${CERES_LIBRARIES}
#   ${PYTHON_LIBRARIES}
#   ${catkin_LIBRARIES} 
#   ${PCL_LIBRARIES} 
#   yaml-cpp
#   gtsam
#   gtsam_unstable
#   # ${MKL_LIBRARIES}
#   # tbb
#   # tbbmalloc
# )

# add_executable(example app/example.cpp)
# target_link_libraries(example ${PROJECT_NAME})

# add_executable(gtsam_temp app/gtsam_temp.cpp)
# target_link_libraries(gtsam_temp ${PROJECT_NAME})

########################################################
################## KAIST_PUBLISHER  ####################
########################################################

file(GLOB KAIST_PUBLISHER_Files
  ${LCISLAM_SRC_DIRS}/kaist_publisher/*.h
  ${LCISLAM_SRC_DIRS}/kaist_publisher/*.cc
  ${LCISLAM_SRC_DIRS}/kaist_publisher/*.cpp
  ${LCISLAM_SRC_DIRS}/kaist_publisher/*.hpp
)

add_library(kaist_publisher SHARED
  ${KAIST_PUBLISHER_Files}
)

target_link_libraries(kaist_publisher
  ${catkin_LIBRARIES}
  ${Boost_LIBRARIES}
)

########################################################
######################  VILENS  ########################
########################################################

file(GLOB VILENS_Files
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*.h
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*.cc
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*.cpp
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*.hpp
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*.h
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*.cc
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*.cpp
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*.hpp
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*/*.h
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*/*.cc
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*/*.cpp
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*/*.hpp
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*/*/*.h
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*/*/*.cc
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*/*/*.cpp
  ${LCISLAM_SRC_DIRS}/lci_slam/vilens/*/*/*/*.hpp
)

add_library(vilens SHARED
  ${VILENS_Files}
)

target_link_libraries(vilens
  ${OpenCV_LIBS}
  ${EIGEN3_LIBS}
  ${Boost_LIBRARIES}
  ${GLOG_LIBRARIES}
  ${CERES_LIBRARIES}
  ${PYTHON_LIBRARIES}
  ${catkin_LIBRARIES} 
  ${PCL_LIBRARIES} 
  yaml-cpp
  gtsam
  gtsam_unstable
  sophus
  # ${MKL_LIBRARIES}
  # tbb
  # tbbmalloc
)

add_executable(vilens_example_online app/vilens_example_online.cpp)
target_link_libraries(vilens_example_online vilens)

add_executable(kaist_vilens_example_offline app/kaist_vilens_example_offline.cpp)
target_link_libraries(kaist_vilens_example_offline vilens kaist_publisher)

add_executable(kaist_publisher_node_online app/kaist_publisher_node_online.cpp)
target_link_libraries(kaist_publisher_node_online kaist_publisher)
